package com.android.taskmanager.battery;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
 import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.taskmanager.R;

public class Battery extends Activity {
	
	private TextView level, voltage, temperature, technology, status, health;
	private ProgressBar progressBar;
	private int progressValue = 0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_battery);
        
        level = (TextView) findViewById(R.id.batterylevel);
        voltage = (TextView) findViewById(R.id.batteryvoltage);
        temperature = (TextView) findViewById(R.id.batterytemperature);
        technology = (TextView) findViewById(R.id.batterytechology);
        status = (TextView) findViewById(R.id.batterystatus);
        health = (TextView) findViewById(R.id.batteryhealth);
        
        this.registerReceiver(myBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        
        progressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
        new Thread(myThread).start();
    }// end onCreate
    
    private BroadcastReceiver myBatteryReceiver = new BroadcastReceiver(){

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
			// get level
			progressValue = intent.getIntExtra("level", 0);
			level.setText("Level: " + String.valueOf(progressValue) + "%");
			// get voltage
			voltage.setText("Voltage: " + String.valueOf((float)intent.getIntExtra("voltage", 0)/1000) + "V");
			// get temperature
			temperature.setText("Temperature: " + String.valueOf((float)intent.getIntExtra("temperature", 0)/10) + "c");
			// get technology
			technology.setText("Technology: " + intent.getStringExtra("technology"));
			
			// get status
			int tempStatus = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
			   String strStatus;
			   if (tempStatus == BatteryManager.BATTERY_STATUS_CHARGING){
			    strStatus = "Charging";
			   } else if (tempStatus == BatteryManager.BATTERY_STATUS_DISCHARGING){
			    strStatus = "Dis-charging";
			   } else if (tempStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
			    strStatus = "Not charging";
			   } else if (tempStatus == BatteryManager.BATTERY_STATUS_FULL){
			    strStatus = "Full";
			   } else {
			    strStatus = "Unknown";
			   }
			status.setText("Status: " + strStatus);
			
			// get health
			int tempHealth = intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);
			   String strHealth;
			   if (tempHealth == BatteryManager.BATTERY_HEALTH_GOOD){
			    strHealth = "Good";
			   } else if (tempHealth == BatteryManager.BATTERY_HEALTH_OVERHEAT){
			    strHealth = "Over Heat";
			   } else if (tempHealth == BatteryManager.BATTERY_HEALTH_DEAD){
			    strHealth = "Dead";
			   } else if (tempHealth == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){
			    strHealth = "Over Voltage";
			   } else if (tempHealth == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){
			    strHealth = "Unspecified Failure";
			   } else{
			    strHealth = "Unknown";
			   }
			health.setText("Health: " + strHealth);
			  
		}// end if intent
		
	  }// end onReceive
    
    };
    
    private Runnable myThread = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			while(progressValue < 100){
				try {
					myHandler.sendMessage(myHandler.obtainMessage());
					Thread.sleep(1000);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		
		Handler myHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				progressBar.setProgress(progressValue);
			}
			
		};
	};
    
}// end class

// Hf3Sc2Yq5xU3